//
//  LKLoginViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/1.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKLoginViewController.h"
#import "LKForgetSecrectViewController.h"
@interface LKLoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTf;
@property (weak, nonatomic) IBOutlet UITextField *secrectTf;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LKLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.loginBtn.layer.cornerRadius = 25;
    self.loginBtn.layer.masksToBounds = YES;
    self.phoneTf.delegate = self;
    self.secrectTf.delegate = self;
//    self.phoneTf.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:@{NSForegroundColorAttributeName:COLOR_HEX(0xD14A43),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:15]}];
    self.secrectTf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName:COLOR_HEX(0xD14A43),NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:15]}];
    [self.phoneTf addTarget:self action:@selector(tfValueChange:) forControlEvents:UIControlEventEditingChanged];
    [self.secrectTf addTarget:self action:@selector(tfValueChange:) forControlEvents:UIControlEventEditingChanged];
//    self.phoneTf.text = @"13817657042";
//    self.secrectTf.text = @"123456";
    
    // Do any additional setup after loading the view from its nib.
}
-(void)tfValueChange:(UITextField *)textField{
    if (self.phoneTf.text.length != 0 && self.secrectTf.text.length != 0) {
        self.loginBtn.backgroundColor = ColorRGB(180, 71, 42);
    }else{
        self.loginBtn.backgroundColor = ColorRGB(137, 105, 93);
    }
}

- (IBAction)secrectTfEnterSetting:(id)sender {
    UIButton *btn = (UIButton *)sender;
    btn.selected = !btn.selected;
    self.secrectTf.secureTextEntry = !self.secrectTf.secureTextEntry;
}


- (IBAction)forgetSecrectAction:(id)sender {
//    LKForgetSecrectViewController *forgetVc = [[LKForgetSecrectViewController alloc] init];
    LKForgetSecrectViewController *forgetVc = [[LKForgetSecrectViewController alloc] initWithNibName:@"LKForgetSecrectViewController" bundle:nil];
    UINavigationController *navVc = [[UINavigationController alloc] initWithRootViewController:forgetVc];
    navVc.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:navVc animated:YES completion:nil];
    
}


- (IBAction)loginAction:(id)sender {

    if ([NSString isBlankString:self.phoneTf.text]) {
        [SVProgressHUD showInfoWithStatus:@"请输入手机号"];
        return;
    }
    if ([NSString isBlankString:self.secrectTf.text]) {
        [SVProgressHUD showInfoWithStatus:@"请输入密码"];
        return;
    }
    NSDictionary *inforDic = [[NSBundle mainBundle] infoDictionary];
    NSString *app_name = [inforDic objectForKey:@"CFBundleDisplayName"];
    NSDictionary *param = @{@"phone":self.phoneTf.text,@"pwd":self.secrectTf.text,@"role_type":@"2",@"version":@"5.6.0"};
    NSLog(@"传参:%@",param);
    [SVProgressHUD showWithStatus:@"登录中..."];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:loginUrl parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqual:@"success"]) {
            [SVProgressHUD dismiss];
            NSDictionary *dic = responseObject[@"userInfo"];
            LKUserInfor *user = [LKUserInfor accountWithDict:dic];
            [LKUserInforTool saveAccount:user];
            [LKSwitchRootVcTool switchRootController];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
